// 引入js和css
var res = document.scripts;
var resPath = res[res.length - 1].src;
  resPath = resPath.substring(0, resPath.lastIndexOf('/') + 1);
var node = document.createElement('script');
  node.async = true;
  node.charset = 'utf-8';
  node.src = resPath + 'jquery.toast.js?v=' + new Date().getTime();
var link = document.createElement('link');
  link.rel = 'stylesheet';
  link.href = resPath + 'jquery.toast.css?v=' + new Date().getTime();
  link.media = 'all';
var head = document.getElementsByTagName('head')[0];
  head.appendChild(node);
  head.appendChild(link);

// 加载中
var loadingInterval;
var thisLoading;
var currentTime = new Date().getTime();
function ToastLoading (msg){
  var options = cloneObj($.toast.options);
  options.text = msg ? msg : '正在加载...';
  options.heading = 'ING';
  options.icon = 'loading';
  options.allowToastClose = false;
  options.hideAfter = false;
  if (typeof thisLoading == 'undefined') {
    thisLoading = $.toast(options);
  } else {
    thisLoading.update(options);
  }
  currentTime = new Date().getTime();
  loadingInterval = window.setInterval(function () {
    if (typeof thisLoading == 'undefined') {
      clearInterval(loadingInterval);
    } else {
      options.heading = 'ING - ' + timePass(currentTime);
      thisLoading.update(options);
    }
  }, 1);
}
function clearLoadingData() {
  thisLoading = window.undefined;
}
function timePass(time) {
  var stamp = new Date().getTime() - new Date(time).getTime();
  return (stamp/1000).toFixed(1) + 's';
}
// error
function ToastErr(msg) {
  ToastShow('400', msg);
}
// warn
function ToastWarn(msg) {
  ToastShow('300', msg);
}
// info
function ToastInfo(msg) {
  ToastShow('200', msg);
}
// success
function ToastOk(msg) {
  ToastShow('100', msg);
}
// 常规
function ToastShow(code, msg, url) {
  var options = cloneObj($.toast.options);
  options.text = msg ? msg : 'no message';
  if (typeof code == 'undefined') { code = '200'; }
  var arrCode = code.split("");
  // 状态码
  if (typeof arrCode[0] == 'undefined' || arrCode[0] == '') { arrCode[0] = '2'; }
  // 是否停留
  if (typeof arrCode[1] == 'undefined' || arrCode[1] == '') { arrCode[1] = '0'; }
  // 是否跳转
  if (typeof arrCode[2] == 'undefined' || arrCode[2] == '') { arrCode[2] = '0'; }
  // 图标样式
  if (arrCode[0] == '1') { options.heading = 'OK'; options.icon = 'success'; }
  else if (arrCode[0] == '3') { options.heading = 'WARNING'; options.icon = 'warning'; }
  else if (arrCode[0] == '4') { options.heading = 'FAIL'; options.icon = 'error'; }
  else { options.heading = 'INFO'; options.icon = 'info'; }
  // 提示时长
  if (arrCode[1] == '1') { options.hideAfter = false; }
  // 页面动作
  if (arrCode[2] == '1') {
    // 页面刷新
    options.hideAfter = 800;
    options.beforeHide = function () { window.location.reload(); }
  } else if (arrCode[2] == '2') {
    // 页面跳转
    if (typeof url != 'undefined') {
      options.hideAfter = 800;
      options.beforeHide = function () {  window.location.href = url; }
    }
  }
  clearLoadingData();
  $.toast().reset('all');
  $.toast(options);
}
//http://www.jq22.com/demo/jquery-toast-plugin-master20161123/demos/index.html#options
/**
 * 实现深复制
 * @param  {[type]} obj [description]
 * @return {[type]}     [description]
 */
var cloneObj = function(obj){
    var str, newobj = obj.constructor === Array ? [] : {};
    if(typeof obj !== 'object'){
        return;
    } else if(window.JSON){
        str = JSON.stringify(obj); //系列化对象
        newobj = JSON.parse(str); //还原
    } else {
        for(var i in obj){
            newobj[i] = typeof obj[i] === 'object' ?
            cloneObj(obj[i]) : obj[i];
        }
    }
    return newobj;
};
